package com.dingqb.mycs;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.base.BaseActivity;
import com.dingqb.mycs.ui.fragment.MainTab1Fragment;
import com.dingqb.mycs.ui.fragment.MainTab2Fragment;
import com.dingqb.mycs.ui.fragment.MainTab3Fragment;
import com.dingqb.mycs.ui.fragment.MainTab4Fragment;

public class MainActivity extends BaseActivity implements OnClickListener {
	private ViewPager viewpager;
	private FragmentPagerAdapter mAdapter;// Fragment适配器，viewpager的适配器
	private List<Fragment> mFragments;// 适配器的数据源

	private LinearLayout mTabWeixin;
	private LinearLayout mTabFrd;
	private LinearLayout mTabAddress;
	private LinearLayout mTabSetting;

	private ImageButton mImgWeixin;
	private ImageButton mImgFrd;
	private ImageButton mImgAddress;
	private ImageButton mImgSetting;

	private Fragment mTab01;
	private Fragment mTab02;
	private Fragment mTab03;
	private Fragment mTab04;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initView();
		initEvent();
		setSelect(0);
	}

	private void initEvent() {
		mTabWeixin.setOnClickListener(this);
		mTabFrd.setOnClickListener(this);
		mTabAddress.setOnClickListener(this);
		mTabSetting.setOnClickListener(this);

	}

	@SuppressWarnings("deprecation")
	private void initView() {
		viewpager = (ViewPager) findViewById(R.id.id_viewpager);
		mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
		mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
		mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);
		mTabSetting = (LinearLayout) findViewById(R.id.id_tab_setting);
		mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
		mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
		mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_img);
		mImgSetting = (ImageButton) findViewById(R.id.id_tab_setting_img);
		mFragments = new ArrayList<Fragment>();// 初始化数据源
		mTab01 = new MainTab1Fragment();  
		mTab02 = new MainTab2Fragment();
		mTab03 = new MainTab3Fragment();
		mTab04 = new MainTab4Fragment();
		// 将4个fragment添加到数据源中
		mFragments.add(mTab01);
		mFragments.add(mTab02);
		mFragments.add(mTab03);
		mFragments.add(mTab04);
		// 初始化适配器
		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public int getCount() {
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int position) {
				return mFragments.get(position);// 通过位置返回不同的fragment
			}
		};
		/*
		 * Android中可变的控件，比如ViewPager，listview,gridview，一个控件对应一个适配器，一个适配器对应一系列的数据源
		 * 数据源一般都是一个list
		 */
		viewpager.setAdapter(mAdapter);// 为viewpager设置一个适配器
		// 实现当viewpager滑动时（滑动页面改变）底部的导航按钮同步改变
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				int currentItem = viewpager.getCurrentItem();
				setTab(currentItem);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.id_tab_weixin:
			setSelect(0);
			break;
		case R.id.id_tab_frd:
			setSelect(1);
			break;
		case R.id.id_tab_address:
			setSelect(2);
			break;
		case R.id.id_tab_setting:
			setSelect(3);
			break;
		default:
			break;
		}

	}

	/*
	 * 将图片设置为亮色的；切换显示内容的fragment
	 */
	private void setSelect(int i) {
		setTab(i);// alt + shift + m可以提取选择的代码为方法
		viewpager.setCurrentItem(i);
	}

	private void setTab(int i) {
		resetImg();
		switch (i) {
		case 0:
			mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
			
			break;
		case 1:
			mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
			break;
		case 2:
			mImgAddress.setImageResource(R.drawable.tab_address_pressed);
			break;
		case 3:
			mImgSetting.setImageResource(R.drawable.tab_settings_pressed);
			break;

		default:
			break;
		}
	}

	private void resetImg() {
		mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
		mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
		mImgAddress.setImageResource(R.drawable.tab_address_normal);
		mImgSetting.setImageResource(R.drawable.tab_settings_normal);
	}
	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ToQuitTheApp();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private boolean isExit = false;

	/**
	 * 双击back 2次退出程序
	 */
	private void ToQuitTheApp() {
		if (isExit) {
			// ACTION_MAIN with category CATEGORY_HOME 启动主屏幕
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);// 使虚拟机停止运行并退出程序
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, "再按一次退出APP", Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(0, 3000);// 3秒后发送消息
		}
	}

	// 创建Handler对象，用来处理消息
	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {// 处理消息
			super.handleMessage(msg);
			isExit = false;
		}
	};
}
